﻿using System;
using System.Windows;
using System.Windows.Controls;
using nRoute.Components;
using nRoute.Samples.Sudoku.Model;
using nRoute.Samples.Sudoku.ViewServices;
using nRoute.ViewServices;
using nRoute.ViewModels;
using nRoute.Samples.Sudoku.ViewModels;

namespace nRoute.Samples.Sudoku.Views
{
    [MapView(typeof(GameViewModel))]
    [MapViewService(typeof(IBoxValueViewService), Lifetime = ViewServiceLifetime.DiscoveredInstance)]
    [MapViewService(typeof(IMessageViewService), Lifetime = ViewServiceLifetime.DiscoveredInstance)]
    public partial class GameView : UserControl, IBoxValueViewService, IMessageViewService
    {
        private const string TITLE_FORMAT = "{0} game"; // "Sudoku ({0})";

        private Action<BoxValue?> _valueCallback;

        public GameView()
        {
            InitializeComponent();
            
            // value converters
            this.SetRelayConverter<BoxValue, string>("BoxValueConverterRelay", 
                (v) => v == BoxValue.None ? string.Empty : Convert.ToInt32(v).ToString());
            this.SetRelayConverter<Box, Visibility>("IsPredefineConverterRelay",
                (b) => b.IsPreDefined ? Visibility.Visible : Visibility.Collapsed);
            this.SetRelayConverter<Box, Visibility>("IsNotPredefineConverterRelay",
                (b) => !b.IsPreDefined ? Visibility.Visible : Visibility.Collapsed);
            this.SetRelayConverter<GameLevel, string>("TitleConverterRelay",
                (l) => string.Format(TITLE_FORMAT, l.ToString()).ToLower());
            
        }

#region Event Handlers

        private void OnSelectBoxValue(Object sender, EventArgs e)
        {
            var _sender = sender as FrameworkElement;
            if (_sender != null && _valueCallback != null)
            {
                // get the value and send the value
                var _value = Convert.ToString(_sender.Tag);
                if (string.IsNullOrEmpty(_value))
                {
                    _valueCallback(null);
                }
                else
                {
                    _valueCallback((BoxValue) Enum.Parse(typeof(BoxValue), _value, true));
                }

                // release the ref and hide the UI
                _valueCallback = null;
                HideBoxValueStoryboard.Begin();
            }
        }

        private void OnMessageClose(object sender, EventArgs e)
        {
            HideMessageStoryboard.Begin();
        }

#endregion

#region IBoxValueViewService Members

        public void GetBoxValue(Action<BoxValue?> valueCallback)
        {
            // basic check
            if (valueCallback == null) throw new ArgumentNullException("valueCallback");

            // save the delegate and show the UI
            _valueCallback = valueCallback;
            ShowBoxValueStoryboard.Begin();
        }

#endregion

#region IMessageViewService Members

        public void ShowMessage(string message)
        {
            if (message == null) throw new ArgumentNullException("message");

            // set and show
            messageText.Text = message;
            ShowMessageStoryboard.Begin();
        }

#endregion

    }
}
